/**
 * @author LiuYan
 * @date 2023/5/15
 *
 * Switch表达式扩展，可以有返回值,
 * java12引入，java13 switch表达式增加yield关键字用于返回结果，作用类似于return，如果没有返回结果则使用break,java14环境可以编译运行
 *
 */
public class SwitchOptimization {

    public static void main(String[] args) {
        String grade = grade(100);
        System.out.println(grade);
        String newGrade= gradeOptimization(60);
        System.out.println(newGrade);
    }

    // Java8 switch()
    public static String grade(int score) {
        score /= 10;
        String grade = "";
        switch (score) {
            case 10:
            case 9:
                grade = "优秀";
                break;
            case 8:
            case 7:
                grade = "良好";
                break;
            case 6:
                grade = "及格";
                break;
            default:
                grade = "不及格";

        }
        return grade;
    }

    // Java14 switch()
    public static String gradeOptimization(int score) {
        score /= 10;
        return switch (score) {
            case 10, 9 -> "优秀";
            case 8, 7 -> "良好";
            case 6 -> "及格";
            default -> "不及格";
        };
    }

}
